package jsu.xxy.coffee3.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jsu.xxy.coffee3.Utils.ExcelOutUtil;
import jsu.xxy.coffee3.domain.Ids;
import jsu.xxy.coffee3.domain.Product;
import jsu.xxy.coffee3.domain.ProductMaterial;
import jsu.xxy.coffee3.mapper.ProductMapper;
import jsu.xxy.coffee3.mapper.ProductMaterialMapper;
import jsu.xxy.coffee3.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductServiceImpl productService;
    @Autowired
    ProductMaterialMapper productMaterialMapper;
    @Autowired
    ProductMapper productMapper;


    @GetMapping("/getProductExcel")
    @Operation(summary = "导出excel，商品全部信息 ,")
    public void getExcel(HttpServletResponse response){
        XSSFWorkbook wb = ExcelOutUtil.getDeviceDataByExcel(productService.getAllProductNoPage());

        String fileName = "deviceData.xlsx";
        OutputStream outputStream = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getAll")
    @Operation(summary = "查询所有商品")
    public List<Product> getAll(){
        return productService.getAll();

    }

    @GetMapping("/getAllProduct")
    @Operation(summary = "管理员获得商品信息")
    public Page<ProductMaterial> getAllProduct(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return productService.getAllProduct(pageNo,pageSize);
    }
    @GetMapping("/getProductByName")
    @Operation(summary = "管理员获得商品信息-name")
    public Page<ProductMaterial> getProductByname(@RequestParam String  name ,@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return productService.getProductByname(name,pageNo,pageSize);
    }
    @GetMapping("/getProductById")
    @Operation(summary = "管理员获得商品信息-Id")
    public Page<ProductMaterial> getProductById(@RequestParam int  id ,@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return productService.getProductById(id,pageNo,pageSize);
    }

    @GetMapping("/getProductByCategory")
    @Operation(summary = "管理员获得商品信息-类别")
    public Page<ProductMaterial> getProductByCategory(@RequestParam int  category ,@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return productService.getProductByCategory(category,pageNo,pageSize);
    }

    @PutMapping("/addProduct")
    @Operation(summary = "添加商品")
    public Boolean add(@RequestBody ProductMaterial productMaterials){
        return productService.addProduct(productMaterials) > 0;
    }
    @DeleteMapping("/deleteByIds")
    @Operation(summary = "删除商品")
    public boolean delete(@RequestParam Integer id){
        return productService.delete(id);
    }
    @DeleteMapping("/deletesByIds")
    @Operation(summary = "批量删除商品")
    public int deletes(@RequestBody List<Integer> ids){
        return productService.deletes(ids);

    }

    @PostMapping("/changeProduct")
    @Operation(summary = "修改商品")
    public Boolean changeProduct(@RequestBody ProductMaterial productMaterials){
        return productService.changeProduct(productMaterials) > 0;
    }


}
